import React, { Component } from 'react'
import { Form, Input, Button,Radio,message } from 'antd';
import {login} from '../api/user';

export default class Login extends Component {

    state = {
        username:'',
        password:''
    }

    onFinish = (values) => {
        console.log('Success:', values);
        //校验成功
        login(values).then(res => {
            if(res.data.code === 1){
                localStorage.setItem("token",res.data.token);
                localStorage.setItem("role_name",res.data.role_name);
                localStorage.setItem('username',values.username);
                message.success('登录成功',1,() => {
                    this.props.history.push('/main/home');
                })
            }else{
                message.error('登录失败',1)
            }
        })
    };
    
    onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };
    render() {
        let {username,password} = this.state;
        return (
            <Form
                onFinish={this.onFinish}
                onFinishFailed={this.onFinishFailed}
                >
                <Form.Item
                    label="Username"
                    name="username"
                    rules={[{ required: true, message: 'Please input your username!' }]}
                >
                    <Input value={username} name="username" onChange={this.change}/>
                </Form.Item>

                <Form.Item
                    label="Password"
                    name="password"
                    rules={[{ required: true, message: 'Please input your password!' }]}
                >
                    <Input value={password} name="password" onChange={this.change}/>
                </Form.Item>

                <Form.Item>
                    <Button type="primary" htmlType="submit">
                        登录
                    </Button>
                </Form.Item>
            </Form>
        )
    }

    change = (e) => {
        this.setState({
            [e.target.name]:e.target.value
        })
    }
}
